package com.ctrip.ec.api.admin.controller.member;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.entity.member.MemberViewEntityEx;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.member.MemberViewDao;
import com.ctrip.ec.biz.entity.member.MemberViewEntity;
import javafx.beans.property.adapter.ReadOnlyJavaBeanBooleanProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("memberView")
public class MemberViewController{
    @Autowired
    private MemberViewDao memberView;

    @PostMapping("list")
    public List<MemberViewEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<MemberViewEntity> list = memberView.list(setting);
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @PostMapping("statistic")
    public List<MemberViewEntityEx> statistic(@RequestBody Map<String,Object> map
                                              ) throws SQLException {

        String from = map.get("from").toString();
        String to= map.get("to").toString();
        Integer viewType = null;
        if(map.containsKey("viewType")){
            viewType = Integer.parseInt(map.get("viewType").toString());
        }


          List<MemberViewEntityEx> list = memberView.list(from,to,viewType);

        return  list;

    }

    @GetMapping("detail/{id}")
    public  MemberViewEntity detail(@PathVariable Long id) throws SQLException {
        return memberView.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody MemberViewEntity entity) throws SQLException {
        memberView.save(entity);
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        memberView.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        memberView.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        memberView.unerase(ids);
    }
}
